#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include "DoubleLinkedListWithHead.h"
//1. 读取一个目录下（包含子目录）所有以.bmp结尾的普通文件的文件名，将其文件名（路径+名字）保存在一个双向链表中
int Get_bmp_filepath(char *dir_name,struct Head*h)
{
    //打开目录
    DIR*dir = opendir(dir_name);
    if(dir == NULL)
    {
        perror("open dir error :");
        return -1;
    }
   

    //读取目录
    struct dirent*p = NULL;
    while(p = readdir(dir))
    {
        if(strcmp(p->d_name,".") == 0 || strcmp(p->d_name,"..") == 0)
        {
            continue;
        }
        char filename[256] = {0};
        strcpy(filename,dir_name);
        strcat(filename,"/");
        strcat(filename,p->d_name);
        //普通文件
        if(p->d_type == DT_REG)
        {
            if(strcmp(p->d_name+strlen(p->d_name)-4,".bmp") == 0)
            {
                Insert_node_y(h, filename);
            }
        }
        //目录文件
        else if(p->d_type == DT_DIR)
        {
            Get_bmp_filepath(filename,h);
        }
    }

    //关闭目录
    closedir(dir);
}

int main(int argc, char**argv)
{
    //创建链表
    struct Head*h = Create_Empty_DoubleLinkedListWithHead();
    Get_bmp_filepath(argv[1],h);

    Print_DoubleLinkedListWithHead(h);


}